package com.cspg.design.ui.map;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.model.CoordUtil;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.utils.CoordinateConverter;
import com.cspg.design.R;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.bean.deviceBean.PointDeviceBean;
import com.cspg.design.presenter.ProjectPresenter;
import com.cspg.design.utils.ShowSystemUIUtil;
import com.cspg.design.utils.Utils;
import com.cspg.design.widget.AlertListDialog;
import com.cspg.design.widget.ToastUtils;

import java.math.BigDecimal;

public class MapActivity extends AppCompatActivity {


    private BaiduMap mBaiduMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        ShowSystemUIUtil.hideSystemUI(this);
        MapView mMapView = (MapView) findViewById(R.id.mv_design_map);
        //隐藏缩放控件
//        mMapView.showZoomControls(false);
        //隐藏比例尺
        mMapView.showScaleControl(false);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);
        // 隐藏logo
        View child = mMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        Button mBtnReturn = findViewById(R.id.bar_btn_return);
        mBtnReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        final String projectId = intent.getStringExtra("projectId");
        if (projectId == null) {
            ToastUtils.toast(this, "项目ID获取失败");
            return;
        }
        mBaiduMap.setOnMapLoadedCallback(() -> {
            Log.e(MapActivity.class.getName(), "地图加载完毕");
            ProjectPresenter projectPresenter = new ProjectPresenter(this);
            projectPresenter.getProjectDevice(projectId, mBaiduMap);
        });
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (marker != null) {
                    AlertDialog alertListDialog = makerDialog(marker);
                    if (alertListDialog != null) {
                        alertListDialog.show();
                    }
                }
                return false;
            }
        });
    }
    //对点设备进行操作
    private AlertDialog makerDialog ( final Marker marker){

        AlertDialog designDailog = new AlertListDialog.ListBuilder(this)
                .setTitle("导航到这里")
                .setIcon(R.drawable.ic_info)
                .setItems(new String[]{"百度地图","高德地图"}, ((dialog, which) -> {
                    dialog.dismiss();
                    LatLng latLng = marker.getPosition();
                    String title = marker.getTitle();
                    if (which == 0) {
                        Uri uri = Uri.parse("baidumap://map/direction?destination=latlng:" + latLng.latitude + "," + latLng.longitude + "|name:" + title + "&mode=driving");
                        MapActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    }else if (which == 1)
                    {
                        latLng = baidu_to_gaode(latLng);
                        Uri uri = Uri.parse("amapuri://route/plan/?dlat="+latLng.latitude+"&dlon="+latLng.longitude+"&dname="+title+"&dev=0&t=0");
                        MapActivity.this.startActivity(new Intent(Intent.ACTION_VIEW, uri));
                    }
                })).setNegativeButton("取消", (dialog, which) -> dialog.dismiss()).create();
        return designDailog;
    }
    /**
     * 将百度坐标转变成火星坐标
     *
     * @param lngLat_bd 百度坐标（百度地图坐标）
     * @return 火星坐标(高德、腾讯地图等)
     */
    public LatLng baidu_to_gaode(LatLng lngLat_bd) {
        double x = lngLat_bd.longitude - 0.0065, y = lngLat_bd.latitude - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        return new LatLng(dataDigit(6, z * Math.sin(theta)), dataDigit(6, z * Math.cos(theta)));
    }

    private double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

    /**
     * 对double类型数据保留小数点后多少位
     * 高德地图转码返回的就是 小数点后6位，为了统一封装一下
     *
     * @param digit 位数
     * @param in    输入
     * @return 保留小数位后的数
     */
    private double dataDigit(int digit, double in) {
        return new BigDecimal(in).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();

    }
}